package entities;

import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Float;

import menu.Settings;
import menu.Settings.Difficulty;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import util.Vector2D;

public class Player extends Entity {

	private static final float MAX_SPEED = 300.0f;
	private static final float CONTROLLED_ACCELERATION = 5000.0f;
	
	private int lives;
	public int playerScore;

	protected Weapon weapon;

	public Player() throws SlickException {
		super("res/enemies/player01.png");
		image = image.getScaledCopy(57, 25);
		maxHealth = 1;
		health = maxHealth;
		playerScore = 0;
		restoreLives();
		
		friction = 7;
		weapon = new SimpleWeapon("res/shot.png", 1, this, 100);
	}
	
	@Override
	public Rectangle2D getHitbox() {
		float w = getImage().getWidth() * 0.6f;
		float h = getImage().getHeight() * 0.6f;
		return new Rectangle2D.Float(getX() + (w / 2.0f), getY() + (h / 2.0f), getImage().getWidth() - w, getImage().getHeight() - h);
	}

	public int getLives() {
		return lives;
	}
	
	public void removeLife() {
		lives --;
	}
	
	public void restoreLives() {
		if (Settings.getDifficulty() == Difficulty.EASY) {
			lives = 5;
		}
		if (Settings.getDifficulty() == Difficulty.MEDIUM) {
			lives = 4;
		}
		if (Settings.getDifficulty() == Difficulty.HARD) {
			lives = 3;
		}
	}
	
	public void moveUp() {
		acceleration = new Vector2D(acceleration.getX(),
				-CONTROLLED_ACCELERATION);
	}

	public void moveDown() {
		acceleration = new Vector2D(acceleration.getX(),
				CONTROLLED_ACCELERATION);
	}

	public void moveLeft() {
		acceleration = new Vector2D(-CONTROLLED_ACCELERATION,
				acceleration.getY());
	}

	public void moveRight() {
		acceleration = new Vector2D(CONTROLLED_ACCELERATION,
				acceleration.getY());
	}

	public void shoot() throws SlickException {
		if (isAlive()) {
			weapon.shoot(new Vector2D(500f, 0).add(velocity.scale(.2f)));
		}
	}
	
	public void addScore(int score) {
		this.playerScore += score;
	}
	
	public int getPlayerScore() {
		return playerScore;
	}
	
	public void resetPlayerScore() {
		playerScore = 0;
	}

	@Override
	public String getName() {
		return "Player";
	}

	@Override
	public void checkEntityPosition(GameContainer gc) {
		if (pos.getX() >= (gc.getWidth() - image.getWidth())) {
			pos = new Vector2D((gc.getWidth() - image.getWidth()), pos.getY());
			velocity = new Vector2D(0, velocity.getY());
		}
		if (pos.getX() <= 0) {
			pos = new Vector2D(0, pos.getY());
			velocity = new Vector2D(0, velocity.getY());
		}
		if (pos.getY() >= (gc.getHeight() - image.getHeight())) {
			pos = new Vector2D(pos.getX(), (gc.getHeight() - image.getHeight()));
			velocity = new Vector2D(velocity.getX(), 0);
		}
		if (pos.getY() <= 0) {
			pos = new Vector2D(pos.getX(), 0);
			velocity = new Vector2D(velocity.getX(), 0);
		}
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (velocity.getLength() > MAX_SPEED) {
			velocity = velocity.getUnitVector().scale(MAX_SPEED);
		}
		acceleration = Vector2D.ZERO_VECTOR;
	}
	
	@Override
	public void reset() {
		super.reset();
		resetPlayerScore();
	}

}
